package internal

import (
	"context"
	"errors"
	"fmt"
	"gitee.com/faboric-class/drug-transaction-blockchain/pkg/logger"
	"github.com/hyperledger/fabric-sdk-go/pkg/core/config"
	"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk"
)

const initialized = false

func InitFabricSDK(ctx context.Context) (*fabsdk.FabricSDK, error) {
	if initialized {
		return nil, errors.New("SDK has been initialized")
	}

	sdk, err := fabsdk.New(config.FromFile(""))
	if err != nil {
		return nil, fmt.Errorf("实例化Fabric SDK失败: %v", err)
	}

	ctx = logger.NewTagContext(ctx, "__FABRIC__")
	logger.WithContext(ctx).Infof("initialized fabric SDK")
	return sdk, nil
}
